module.exports = {
  /**
   * 权限模块
   * 权限标识：
   *  - READ_PERMISSION
   *  - ADD_PERMISSION
   *  - DELETE_PERMISSION
   *  - UPDATE_PERMISSION
   */

  /**
   * 获取权限列表
   */
  async getPermission() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)

    const skip = params.pageNum == 1 ? 0 : params.pageSize * (params.pageNum - 1)
    const res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-permissions')
      .skip(skip)
      .limit(params.pageSize)
      .get({
        getCount: true
      })

    return res
  },

  /**
   * 添加权限
   */
  async addPermission() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)

    // 检查必要字段
    if (!params || !params.permission_id || !params.permission_name) {
      return {
        errCode: 400,
        message: '权限标识和权限名称不能为空'
      }
    }

    // 检查权限ID是否已存在
    const existRole = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-roles')
      .where({ permission_id: params.permission_id })
      .count()

    if (existRole.total > 0) {
      return {
        errCode: 400,
        message: '权限标识已存在'
      }
    }

    //剔除报错字段
    delete params._id
    // 插入数据
    const res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-permissions')
      .add(params)

    return res
  },

  /**
   * 修改权限
   */
  async updatePermission() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)
    const _id = params._id

    if (!_id) {
      return {
        errCode: 400,
        message: '权限ID不能为空'
      }
    }

    delete params._id
    const res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-permissions')
      .where({ _id })
      .update(params)

    if (res.updated === 0) {
      return {
        errCode: 500,
        message: '权限更新失败'
      }
    }

    return res
  },

  /**
   * 删除权限
   */
  async deletePermission() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)
    if (!params.permission_id) {
      return {
        errCode: 400,
        message: '权限ID不能为空'
      }
    }

    // 检查权限是否被角色使用
    const roles = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-roles')
      .get()

    if (Array.isArray(roles.data) && roles.data.length) {
      const usePermission = [
        ...new Set(roles.data.map((i) => i.permission).flat(Number.MAX_SAFE_INTEGER))
      ]

      if (usePermission.includes(params.permission_id)) {
        return {
          errCode: 400,
          message: '当前权限已被角色使用，无法删除'
        }
      }
    }

    const res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-permissions')
      .where({ permission_id: params.permission_id })
      .remove()

    if (res.deleted === 0) {
      return {
        errCode: 404,
        message: '权限不存在'
      }
    }

    return res
  }
}
